class Loader {
    constructor(getKey, createLoadPromise, context) {
        this.promises = {};
        this.getKey = getKey;
        this.context = context || window;
        this.createLoadPromise = createLoadPromise;
    }

    load(loaders) {
        let loaderList = (Array.prototype.isArray(loaders)) ? loaders : Array.prototype.slice.call(arguments, 1);
        let _promises = [];
        loaderList.forEach(loader=> {
            let key = this.getKey(loader);
            let _promise = this.promises[key] || (this.promises[key] = this.createLoadPromise.call(this.context, loader));
            _promises.push(_promise);
        });
        return $.when.apply($, _promises);
    }
}

export {Loader};